package com.lm56.common;

import javax.servlet.ServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Kelvin Yeung
 */
@SuppressWarnings("rawtypes")
public class Utils {



    private static Pattern mobilePhonePattern;
    private static Pattern emailPattern;
    private static Pattern passwordPattern;

    static {
        mobilePhonePattern = Pattern.compile("^1[0-9]{10}$");
        emailPattern = Pattern.compile("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+");

        passwordPattern = Pattern.compile("^[\\w.]{6,18}$");//由字母或数字组成，长度在6~18之间，只能包含字符、数字和下划线
    }

    public static boolean isMobilePhoneNo(String str) {
        Matcher m = mobilePhonePattern.matcher(str);
        return m.matches();
    }

    public static boolean isEmail(String str) {
        Matcher m = emailPattern.matcher(str);
        return m.matches();
    }

    public static boolean isValidPassword(String pwd){
        Matcher m = passwordPattern.matcher(pwd);
        return m.matches();
    }


    /**
     * 判断不为空
     *
     * @param obj
     * @return
     */
    public static boolean isNotEmpty(Object obj) {
        if (obj == null) {
            return false;
        } else if ("".equals(obj)) {
            return false;
        }
        return true;
    }

    /**
     * 判断为空
     *
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        return !isNotEmpty(obj);
    }

    /**
     * 获取元素在数组中的位置
     *
     * @param arr
     * @param value2
     * @return
     */
    public static int indexOfArr(String[] arr, String value2) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].equals(value2)) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 将表单传递的参数转成map
     *
     * @param request
     * @return
     */
    public static Map<String, Object> getRequestMap(ServletRequest request) {
        Enumeration rnames = request.getParameterNames();
        Map<String, Object> map = new HashMap<String, Object>();
        for (Enumeration e = rnames; e.hasMoreElements(); ) {
            String thisName = e.nextElement().toString();
            if (!"sortType".equals(thisName.trim())) {
                map.put(thisName, request.getParameter(thisName));
            }
            // String thisValue = request.getParameter(thisName);
            // System.out.println(thisName + "-------" + thisValue);

        }
        return map;
    }

    public static String getFilterNull(Object s) {
        if (isNotEmpty(s)) {
            return s.toString();
        }
        return "";
    }

    /**
     * 转数组
     *
     * @param title
     * @return
     */
    public static String[] getMembersTitle(List<Map<String, Object>> title) {
        String[] s = new String[title.size() + 6];
        int c = 1;
        for (int i = 1; i < s.length; i++) {
            if (i != 1 && i % 4 == 0 && c < 4) {
                s[i] = "第" + c + "季度";
                c++;
            } else {
                if (i - c < title.size())
                    s[i] = title.get(i - c).get("title") + "";
            }
        }
        return s;
    }

    public static void main(String[] args) {
        String phone = "13560248725";
        String emial = "abc@abc.com";
        System.out.println("is phone:" + Utils.isMobilePhoneNo(phone));
        System.out.println("is phone:" + Utils.isMobilePhoneNo(phone));

    }
}
